package com.hongye.why.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongye.why.R;
import com.hongye.why.base.BaseActivity;
import com.hongye.why.util.BaseUtils;


/**
 * @description 标题栏
 * @date 2014-12-20
 */
public class TitleView extends FrameLayout {

    private TextView txTitle;
    private Context context;
    private LinearLayout layLeft, layRight;
    private RelativeLayout layout_center;

    //	private View titleView;
    public LinearLayout getLayRight() {
        return layRight;
    }

    private TextView textRight;

    private static final long DEFAULT_ANIM_DURATION = 100;
    private static int menuId = Integer.MAX_VALUE / 2;

    private ImageView imgLoading;

    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, AttributeSet attrs) {

        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.view_title, this);
        this.context = context;
        setBackgroundColor(0xff58b95d);

        txTitle = (TextView) findViewById(R.id.tx_title);
        layLeft = (LinearLayout) findViewById(R.id.layout_left);
        layRight = (LinearLayout) findViewById(R.id.layout_right);
        textRight = (TextView) findViewById(R.id.text_right);
        layout_center = (RelativeLayout) findViewById(R.id.layout_center);
        imgLoading = (ImageView) findViewById(R.id.img_loading);
        setLoadingVisibility(false);

    }

    public ViewGroup getCenterView() {
        return layout_center;

    }

    public void setCenterView(View v) {
        layout_center.removeAllViews();
        layout_center.addView(v);
    }


    public void setLoadingVisibility(boolean visible) {
//		boolean useLoadingUI = false;
//		if(!useLoadingUI) {
//			return;
//		}

        if ((visible && imgLoading.getVisibility() == View.VISIBLE)
                || (!visible && imgLoading.getVisibility() != View.VISIBLE)) {
            // 这种可以return。 已经打开了还要求打开， 已经关闭了还要求关闭
            return;
        }

        imgLoading.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);

        AnimationDrawable animationDrawable = (AnimationDrawable) imgLoading.getBackground();
        if (visible) {
            animationDrawable.start();
        } else {
            animationDrawable.stop();
        }
    }

    public void setBackGround(int color) {
        setBackgroundColor(color);
        getBackground().setAlpha(127);//0~255透明度值

    }

    public void setBackGround2(int color) {
        setBackgroundColor(color);
    }

    public void setBackgroundColor2(int color) {
        setBackgroundColor(color);
    }

    public void setIntercept(boolean intercept) {
        for (int i = 0; i < layLeft.getChildCount(); i++) {
            layLeft.getChildAt(i).setEnabled(intercept);
        }
        for (int i = 0; i < layRight.getChildCount(); i++) {
            layRight.getChildAt(i).setEnabled(intercept);
        }
    }

    public void setTitle(final int textId) {
        txTitle.setText(textId);
    }

    public TextView getTitleView() {
        return txTitle;
    }

    public void setTitle(final int textId, final Animation exit, final Animation enter, final long duration) {
        txTitle.startAnimation(exit);
        txTitle.postDelayed(new Runnable() {
            @Override
            public void run() {
                txTitle.setText(textId);
                txTitle.clearAnimation();
                txTitle.startAnimation(enter);
            }
        }, duration);
    }

    public void setTitle(String text) {
        txTitle.setText(text);
    }

    public View addLeftDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener) {
        return addLeftDrawableMenu(context, drawableId, width, height, onMenuClickListener, false);
    }

    public View setLeftDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener) {
        layLeft.removeAllViewsInLayout();
        return addLeftDrawableMenu(context, drawableId, width, height, onMenuClickListener, false);
    }

    public View addLeftDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener,
                                    boolean useDefaultAnim) {
//		closeHideSoftInput(context);
        View view = createDrawableMenu(context, drawableId, width, height, onMenuClickListener);
        LinearLayout.LayoutParams LP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        int left = BaseUtils.dip(getContext(), 10);
        int right = BaseUtils.dip(getContext(), 10);
        view.setPadding(left, 0, right, 0);
        view.setLayoutParams(LP);
        addViewToLeftLayout(view, useDefaultAnim);
        setIdTagForMenu(view);
        return view;
    }

    public View addLeftDrawableFinish(final Activity context) {
        return addLeftDrawableFinish(context, false);
    }

    public View addLeftDrawableFinish(final Activity context, boolean useDefaultAnim) {
        // int size = BaseUtils.dip(context, 10);
        View view = createDrawableMenu(context, R.drawable.icon_back, 20, 20, new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                ((BaseActivity)context).closeHideSoftInput();
                context.finish();
            }
        });
        LinearLayout.LayoutParams LP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        int left = BaseUtils.dip(getContext(), 10);
        int right = BaseUtils.dip(getContext(), 20);
        view.setPadding(left, 0, right, 0);
        view.setLayoutParams(LP);
        addViewToLeftLayout(view, useDefaultAnim);
        setIdTagForMenu(view);
        return view;

    }

    public View addRightDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener) {
        return addRightDrawableMenu(context, drawableId, width, height, onMenuClickListener, false);
    }

    public View setRightDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener) {
        layRight.removeAllViewsInLayout();
        return addRightDrawableMenu(context, drawableId, width, height, onMenuClickListener, false);
    }

    public View addRightDrawableMenu(Context context, int drawableId, int width, int height, OnClickListener onMenuClickListener,
                                     boolean useDefaultAnim) {
        View view = createDrawableMenu(context, drawableId, width, height, onMenuClickListener);
        LinearLayout.LayoutParams LP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        int left = BaseUtils.dip(getContext(), 10);
        int right = BaseUtils.dip(getContext(), 10);
        view.setPadding(left, 0, right, 0);
        view.setLayoutParams(LP);
        addViewToRightLayout(view, useDefaultAnim);
        setIdTagForMenu(view);
        return view;
    }

    public void addRightMenu(View right) {
        layRight.removeAllViewsInLayout();
        layRight.addView(right);
    }

    public void addRightMenuWidthOriginal(View right) {
        layRight.addView(right);
    }

    public View addRightTextMenu(Context context, int textId, int paddings, OnClickListener onMenuClickListener) {
        return addRightTextMenu(context, textId, paddings, onMenuClickListener, false);
    }

    public View addRightTextMenu(Context context, int textId, int paddings, OnClickListener onMenuClickListener, boolean useDefaultAnim) {
        Button view = createTextMenu(context, textId, paddings, onMenuClickListener);
        addViewToRightLayout(view, useDefaultAnim);
        return view;
    }

    public Button createTextMenu(Context context, int textId, int paddings, OnClickListener onMenuClickListener) {
        Button button = new Button(context);
        button.setText(textId);
        button.setTextSize(16);
        button.setGravity(Gravity.CENTER);
        button.setTextColor(Color.WHITE);
        button.setBackgroundColor(Color.TRANSPARENT);

        LinearLayout.LayoutParams btnLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        paddings = BaseUtils.dip(context, paddings);
        button.setPadding(paddings, paddings, paddings, paddings);
        button.setLayoutParams(btnLP);

        button.setOnClickListener(onMenuClickListener);
        return button;
    }

    /**
     * 在标题栏右侧加文字
     *
     * @param context
     * @param onMenuClickListener
     * @param text
     */
    public void addRightText(Activity context, OnClickListener onMenuClickListener, String text) {
        textRight.setVisibility(View.VISIBLE);
        textRight.setOnClickListener(onMenuClickListener);
        textRight.setText(text);
    }

    public void updateRightText(String text) {
        textRight.setText(text);
    }

    public View addLeftMenu(Context context, View view, View.OnClickListener onMenuClickListener) {
        return addLeftMenu(context, view, onMenuClickListener, false);
    }

    public View addLeftMenu(Context context, View view, View.OnClickListener onMenuClickListener, boolean useDefaultAnim) {
        view.setOnClickListener(onMenuClickListener);
        addViewToLeftLayout(view, useDefaultAnim);
        setIdTagForMenu(view);
        return view;
    }

    public View addLeftMenu(Context context, View view) {
        addViewToLeftLayout(view, false);
        setIdTagForMenu(view);
        return view;
    }

    public View addRightMenu(Context context, View view) {
        addViewToRightLayout(view, false);
        setIdTagForMenu(view);
        return view;
    }

    public FrameLayout createDrawableMenu(Context context, int drawableId, int width, int height, View.OnClickListener onMenuClickListener) {
        FrameLayout layF = (FrameLayout) LayoutInflater.from(getContext()).inflate(R.layout.view_title_part_drawable_menu, null, false);
        ImageView imageView = (ImageView) layF.findViewById(R.id.imageView);
        imageView.setBackgroundResource(drawableId);

        FrameLayout.LayoutParams btnLP = new FrameLayout.LayoutParams(BaseUtils.dip(context, width), BaseUtils.dip(context, height));
        imageView.setLayoutParams(btnLP);
        btnLP.gravity = Gravity.CENTER_VERTICAL;
        layF.setOnClickListener(onMenuClickListener);
        return layF;

    }

    public void removeAllMenu(boolean useDefaultAnim) {
        removeAllLeftMenu(useDefaultAnim);
        removeAllRightMenu(useDefaultAnim);
    }

    public void removeAllLeftMenu(boolean useDefaultAnim) {
        if (useDefaultAnim) {
            long duration = DEFAULT_ANIM_DURATION;
//			layLeft.startAnimation(AnimUtils.init().addAlpha(1f, 0.3f, 0, duration, new LinearInterpolator(), 1).create());
            layRight.postDelayed(new Runnable() {
                @Override
                public void run() {
                    layLeft.removeAllViews();
                }
            }, duration);
        }
        layLeft.removeAllViews();
    }

    public void removeAllRightMenu(boolean useDefaultAnim) {
        if (useDefaultAnim) {
            long duration = DEFAULT_ANIM_DURATION;
//			layRight.startAnimation(AnimUtils.init().addAlpha(1f, 0.3f, 0, duration, new LinearInterpolator(), 1).create());
            layRight.postDelayed(new Runnable() {
                @Override
                public void run() {
                    layRight.removeAllViews();
                }
            }, duration);
        } else {
            layRight.removeAllViews();
        }
    }

    private void addViewToLeftLayout(final View view, boolean useDefaultAnim) {
        if (useDefaultAnim) {
            final long duration = DEFAULT_ANIM_DURATION;
            layLeft.postDelayed(new Runnable() {
                @Override
                public void run() {
                    layLeft.addView(view);
//					layLeft.startAnimation(AnimUtils.init().addAlpha(0.3f, 1f, 0, duration, new LinearInterpolator(), 1).create());
                }
            }, duration);
        } else {
            layLeft.addView(view);
        }
    }

    private void addViewToRightLayout(final View view, boolean useDefaultAnim) {
        if (useDefaultAnim) {
            final long duration = DEFAULT_ANIM_DURATION;
            layRight.postDelayed(new Runnable() {
                @Override
                public void run() {
                    layRight.addView(view);
//					layRight.startAnimation(AnimUtils.init().addAlpha(0.3f, 1f, 0, duration, new LinearInterpolator(), 1).create());
                }
            }, duration);
        } else {
            layRight.addView(view);
        }
    }

    private void setIdTagForMenu(View view) {
        view.setId(menuId);
        menuId++;
    }

    public TextView getTextRight(){
        return textRight;
    }
}
